<!DOCTYPE html>
<html>
<head>
<head/>
<div th:include="base-libs-easyui :: base-libs-easyui" />
<title>用户列表</title>
<style type="text/css">
body{
	padding: 0px 10px;
}
</style>
</head>
<script th:inline="javascript">
    /**读取request中的初始化数据*/
    var requestData = [[${requestData}]];
</script>
<script type="text/javascript">

	var userState = [
		{
		    XMDM: "0"
			,XMMC: "禁用"
		}
		,{
            XMDM: "1"
            ,XMMC: "启用"
        }
    ]

$(function() {
 	initView();
 	$("#btnQuery").trigger("click");
});

function queryForm() {
    $("#dgForm").datagrid({url: contextRoot + "/user/queryPageUser.service"});
}

function initView()
{
	$("#dgForm").datagrid({
		idField : "USERID",
		striped : true,
        fitColumns : true,
        fit : true,
        border : false,
        rownumbers: true,
        singleSelect: true,
		toolbar : "#tb",
        pagination: true,
		columns : [[
			{
				field : "YLJGMC",
				title : "医疗机构名称"
				,width: 6
			},
			{
				field : "KSMC",
				title : "科室名称"
                ,width: 3
			},
            {
                field : "YSGH",
                title : "工号"
                ,width: 3
            },
			{
				field : "XM",
				title : "姓名"
                ,width: 4
			},
			{
				field : "USERNAME",
				title : "用户名"
                ,width: 4
			},
            {
                field : "ROLENAME",
                title : "角色名"
                ,width: 4
            },
            {
                field : "STATE",
                title : "状态"
                ,width: 2
                ,formatter : function(value, row, index){
                    var name = "";
                    $.each(userState, function(){
                        if(this["XMDM"] == value){
                            name = this["XMMC"];
                            return false;
						}
					})
				return name;
				}
            },
			{
				field : "CZ",
				title : "操作"
                ,width: 4
                ,formatter : function(value, row, index){
				    if(StringUtils.isEmpty(row["SUPER"])){
                        var tableToolBar = $($("#tableToolBar").html());
                        var STATE = row["STATE"];
                        if(STATE == "1"){
                            $(tableToolBar).find("a[action='enable']").remove();
                        }else if(STATE == "0"){
                            $(tableToolBar).find("a[action='disable']").remove();
                        }else if(STATE == ""){
                            $(tableToolBar).find("a[action='disable']").remove();
                            $(tableToolBar).find("a[action='enable']").remove();
                            $(tableToolBar).find("a[action='delete']").remove();
                        }
                        return $(tableToolBar).prop("outerHTML").replace(/USERID/g, row["USERID"]);
					}
				}
			}
		]]
        ,onBeforeLoad: function(queryParams) {
            var url = $(this).datagrid("options")["url"];
			/*URL不等空时再发送请求*/
            if (!StringUtils.isEmpty(url)) {
                queryParams["pageSize"] = queryParams["rows"];
                queryParams["pageNumber"] = queryParams["page"];
                /*获取查询条件*/
                var formParams = getQueryFormValue();
                queryParams = $.extend(queryParams, formParams);
                return true;
            }else{
                return false;
            }
        }
	});
    $("body").on("click", ".tableToolBarBtn", function(){
        var action = $(this).attr("action");
        var USERID = $(this).attr("id");
        $("#dgForm").datagrid("selectRecord", USERID);
        var item = $("#dgForm").datagrid("getSelected");
        if("assignUser" == action){
            assignUserHandler(item);
		}else if("delete" == action){
            deleteUserHandler(item);
        }else if("enable" == action || "disable" == action){
            updateStateUserHandler(item, action);
        }
    })
    var readonly = !requestData["isManager"];
    $("#combogridYljg").combogrid({
        height: 30
        ,width: 200
        ,panelWidth: 500
        ,singleSelect: true
        ,pagination: true
        ,idField: 'PTJGID'
        ,textField: 'YLJGMC'
        ,mode: "remote"
        ,readonly: readonly
        ,hasDownArrow: !readonly
        ,url: contextRoot + "/yljgxx/queryPageSummary.service"
        ,delay: 1000
        ,reversed: false
        ,columns:[[
            {
                field:'YLJGMC'
                ,title:'机构名称'
                ,width:240
            }
            ,{
                field:'XZQHMC'
                ,title:'行政区划'
                ,width:80
            }
            ,{
                field:'WSJGLBMC'
                ,title:'机构类别'
                ,width:80
            }
            ,{
                field:'YYDJMC'
                ,title:'机构等级'
                ,width:80
            }
        ]]
        ,onBeforeLoad: function(queryParams) {
            var url = $(this).datagrid("options")["url"];
			/*URL不等空时再发送请求*/
            if (!StringUtils.isEmpty(url)) {
                queryParams["pageSize"] = queryParams["rows"];
                queryParams["pageNumber"] = queryParams["page"];
                queryParams["YLJGMC"] = queryParams["q"];
                return true;
            }else{
                return false;
            }
        }
    });

	if(!requestData["isManager"]){
		$(".jgTd").hide();//机构名称查询条件
	}

	/*用户状态*/
	$("#STATE").createOptions(userState, {needNull: "true"});
}

/**获取查询表单的值*/
function getQueryFormValue(){
    var params = {};
    var ary = $("#queryForm").serializeArray();
    $.each(ary, function(index, item){
        params[item["name"]] = item["value"];
	})
	/*combobox取值*/
    var PTJGID = $("#combogridYljg").combobox("getValue");
    params["PTJGID"] = PTJGID;
    return params;
}

/**删除*/
function deleteUserHandler(item){
    $.QuickAlert.alertConfirm({content:"将彻底删除该用户，是否继续删除", callback: function(){
        var url = contextRoot + "/user/delete";
        $.QuickRemote.AjaxJson(url, function(result){
            if(result["state"])
            {
                $.QuickAlert.alertNormal("删除成功");
                $("#btnQuery").trigger("click");
            }
            else
            {
                $.QuickAlert.alertNormal("删除失败");
            }
        }, {USERNAME: item["USERNAME"], USERID: item["USERID"]})
    }})
}

/**更新用户状态*/
function updateStateUserHandler(item, action){
    var state = "0";
    var actionTxt = "禁用";
    var confirmTxt = "该用户将无法使用系统，是否继续";
    if("enable" == action){
        state = "1";
        actionTxt = "启用";
        confirmTxt = "用户将恢复使用系统，是否继续";
    }
    $.QuickAlert.alertConfirm({
		content: confirmTxt,
		callback: function(){
            var url = contextRoot + "/user/updateUserState";
            $.QuickRemote.AjaxJson(url, function(result){
                if(result["state"])
                {
                    $.QuickAlert.alertNormal(actionTxt+"成功");
                    $("#btnQuery").trigger("click");
                }
                else
                {
                    $.QuickAlert.alertNormal(actionTxt+"失败");
                }
            }, {USERID: item["USERID"], STATE: state})
    	}
    })
}

/**分配用户*/
function assignUserHandler(item){
	console.log(JSON.stringify(item));
    StoreCache.setCache("cache_yljgry", item);
    var type = item["WSJGLBDM"];
    if(!StringUtils.isEmpty(item["SUPER"])){
        type = "";
	}
    $.QuickUrlUtils.redirectRootUrl("/user/manager.html?WSJGLBDM=" + type);
}

/**查询按钮点击回调*/
function btnQueryHandler() {
    queryForm();
}

/**查询按钮点击回调*/
function btnQueryHandler() {
	queryForm();
}

</script>

<body class="easyui-layout">
	<div region="north" style="height: 41px; border: none; overflow: hidden; background-color: #f9f9f9;">
		<div style="width: 100%; height: 100%; margin-left: 30px">
			<h3 style="line-height: 41px; margin: 0px; font-size: 18px;font-weight: bold;">用户列表</h3>
		</div>
	</div>
	<div region="center">
		<div id="tb" style="padding:8px 20px;">
			<form id="queryForm">
				<table>
					<tr>
						<td class="query-text jgTd">机构名称</td>
						<td class="query-content jgTd">
							<input id="combogridYljg">
						</td>
						<td class="query-text">用户名</td>
						<td class="query-content">
							<input name="USERNAME" class="query-input">
						</td>
						<td class="query-text">姓名</td>
						<td class="query-content">
							<input name="XM" class="query-input">
						</td>
						<td class="query-text">角色名</td>
						<td class="query-content">
							<input name="JSM" class="query-input">
						</td>
						<td class="query-text">用户状态</td>
						<td class="query-content">
							<select id="STATE" name="STATE" class="query-input" style="width: 80px;"></select>
						</td>
						<td>
							<button id="btnQuery" class="btn btn-default" type="button" onclick="btnQueryHandler()">查询</button>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<table id="dgForm"></table>
	</div>
</body>
<script type="text/html" id="tableToolBar">
	<div>
		<a class="tableToolBarBtn" action="disable" id=USERID href="javascript:void(0)" title="禁用" style="color: #00c297;">禁用</a>
		<a class="tableToolBarBtn" action="enable" id=USERID href="javascript:void(0)" title="启用" style="color: #00c297;">启用</a>
		<!--<a class="tableToolBarBtn" action="delete" id=USERID href="javascript:void(0)" title="删除用户" style="color: #00c297;">删除用户</a>-->
		<a class="tableToolBarBtn" action="assignUser" id=USERID href="javascript:void(0)" title="分配用户" style="color: #00c297;">分配用户</a>
	</div>
</script>
</html>



